<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;
use App\Util\Url;

class OrderExporter extends ExcelExporter implements WithMapping
{

    protected $fileName = '商品订单列表.xlsx';

    protected $columns = [
        "id" => "ID",
        "order_goods.goods_name" => "商品名称",
        "order_goods.goods_img" => "商品图片",
        "sn" => "订单编号",
        "order_source" => "商品类型",
        "distributor.name" => "所属供应商",
        "work.work_name" => "所属医护工作室",
        "order_goods.product_standard" => "商品规格",
        "order_goods.product_remark" => "商品备注",
        "orderaddress.name" => "收货人姓名",
        "orderaddress.phone" => "联系方式",
        "orderaddress.detail" => "地址",

        "nums" => "商品数量",
        "created_at" => "创建时间",
        "pay_time" => "付款时间",
        "order_status" => "订单状态",
        "total_price" => "价格",
    ];

    /**
     * @param $toArray
     * @return array
     * 获取全部数据
     */
    public function getData($toArray = true)
    {
        $data = parent::getData($toArray);
        $result = [];
        foreach ($data as $val) {
            $result[$val['id']] = $val;
        }
        return $result;
    }

    /**
     * @param $id
     * @return mixed|string
     * 所属供应商
     */
    public function SuppliersName($id)
    {
        $res = $this->getData();
        if (!empty($res[$id])) {
            return $res[$id]['distributor']['name'] ?? '';
        } else {
            return '';
        }
    }

    /**
     * @param $id
     * @return mixed|string
     * 所属工作室
     */
    public function Work($id)
    {
        $res = $this->getData();
        if (!empty($res[$id])) {
            return $res[$id]['work']['work_name'] ?? '';
        } else {
            return '';
        }
    }

    /**
     * @param $url
     * @return string
     */
    public function getImage($url)
    {
        $src = Url::getHttpHost() . 'upload/' . $url;
        return $src;
    }

    public function map($row): array
    {
        $option = [
            0 => '未完成',
            1 => '已完成',
            2 => '申请售后',
            3 => '售后完成',
            4 => '取消订单',
        ];
        return [
            $row->id,
            $row->order_goods->goods_name ?? '',                        //商品名称
            $row->order_goods->goods_img = $this->getImage($row->order_goods->goods_img) ?? '',//商品图片
            $row->sn,
            $row->order_source == 1 ? '工作室' : '供应商',                //商品类型
            $row->distributor->name ?? $this->SuppliersName($row->id),  //所属供应商
            $row->work->work_name ?? $this->Work($row->id),             //所属医护工作室

            $row->order_goods->product_standard ?? '',                  //商品规格
            $row->order_goods->product_remark ?? '',                    //商品备注
            $row->orderaddress->name ?? '',                             //收货人姓名
            $row->orderaddress->phone ?? '',                            //联系方式
            $row->orderaddress->detail ?? '',                           //地址
            $row->nums,
            $row->created_at,
            $row->pay_time,
            $row->order_status = $option[$row->order_status] ?? '',
            $row->total_price,
        ];
    }
}
